package com.common.utils.db;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.*;
import java.util.Properties;

/**
 * @Author: zengliang
 * @Description: 数据库连接工具类
 * @Date:Created in 18:03 2022/4/8
 * @Modified By:
 */
public class DB {

    //数据库地址
    private static String url;
    //数据库
    private static String database;
    //用户名
    private static String username;
    //密码
    private static String password;
    //驱动
    private static String driver;

    private static Properties p = new Properties();

    static{
        try {
            InputStream in = DB.class.getClassLoader().getResourceAsStream("config/db.properties");
            //据说这一句使用来读取中文用的
            BufferedReader bf = new BufferedReader(new InputStreamReader(in));
            p.load(bf);
            driver = p.get("driver").toString().trim();
            //加载数据库驱动
            Class.forName(driver);
        }
        catch (ClassNotFoundException | IOException e) {
            e.printStackTrace();
        }
    }

    public DB() {}

    public static Connection getConn(){
        Connection conn = null;
        try {
            url = p.get("url").toString().trim();
            database = p.get("database").toString().trim();
            username = p.get("username").toString().trim();
            password = p.get("password").toString().trim();
            conn = DriverManager.getConnection(url+"/"+database, username,password);//连接数据库
        }
        catch(SQLException e){
            e.printStackTrace();
        }
        return conn;
    }

    public static void closeConn(Connection conn){
        try {
            if(conn != null ){
                conn.close();
                conn = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static Statement getStmt(Connection conn){
        Statement stmt = null;
        try {
            stmt = conn.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return stmt;
    }

    public static PreparedStatement getPStmt(Connection conn, String sql){
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pstmt;
    }

    public static void closeStmt(Statement stmt){
        try {
            if(stmt != null ){
                stmt.close();
                stmt = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public static ResultSet executeQuery(Statement stmt,String sql){
        ResultSet rs = null;
        try {
            rs = stmt.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }

    public static void closeRs(ResultSet rs){
        try {
            if(rs != null ){
                rs.close();
                rs = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static ResultSet executeQuery(Connection conn,String sql){//重载
        ResultSet rs = null;
        try {
            rs = conn.createStatement().executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }
}

